package org.jiamandan.com.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelId;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.cache.CacheNames;
import org.jiamandan.com.config.TcpInboundAdapter;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.constant.JSYConstant;
import org.jiamandan.com.constant.YxConstant;
import org.jiamandan.com.dto.RemoteStartDto;
import org.jiamandan.com.entity.ChargeRule;
import org.jiamandan.com.entity.RechargeGun;
import org.jiamandan.com.entity.RechargePile;
import org.jiamandan.com.entity.TimeModel;
import org.jiamandan.com.service.IRechargePileService;
import org.jiamandan.com.strategy.impl.car.cmd.YxRemoteStartService;
import org.jiamandan.com.utils.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class RechargePileServiceImpl implements IRechargePileService {
    RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
    @Resource
    private YxRemoteStartService yxRemoteStartService;

    @Override
    //TODO 严重声明，本测试是两路测试，测试10路需要更改code
    public void testJsy2charge(String pileNo, String gunNo, Integer isOpen) {
        BigDecimal chargeAmt = new BigDecimal(0);
        long id = IdentityIdUtil.getFlowIdInstance().nextId();
        String orderNo = String.valueOf(id);
        Channel channel = getChannel(pileNo);
        if(channel==null){
            throw new RuntimeException("channel不能为空");
        }
        StringBuilder payload = new StringBuilder();
        payload.append("00")//费率模式  、余额模式
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(chargeAmt.multiply(BigDecimal.valueOf(100)).intValue()), 8, "0")))
                    .append(StringUtils.leftPad(gunNo, 2, "0")) //端口号
                //充电命令=0(停止充电)，=1(开始充电)设备充电与否依据此字段
                .append(StringUtils.leftPad(String.valueOf(isOpen), 2, "0"))    //充电的命令
                .append("0000")
                .append(StringUtils.leftPad("146974540608835584", 32, "0"))
                //最大充电时长 先按照 最高功率计算费用下发最大时间，后06指令获取实际最高功率重新计算时间08指令下发
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(9850), 4, "0")))
                //根据设备不同2口设备1800~2000W，10口设备900W
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(900 * 10), 4, "0")))
                .append("01")
                .append("00")
                .append("0000");//额外增加的浮充时间，单位秒，如果下发的是0xFFFF，则为取消浮充
        RechargePile pile = new RechargePile();
        pile.setCode(5);        //TODO 严重声明，本测试是两路测试，测试10路需要更改code
        pile.setPileNo(pileNo);
        String replyMsg = ZDObjectUtil.ydsplicingMessage(payload.toString(), pile, gunNo, BizConstant.Cmd.CMD_YD_82);
        log.info("JMD-YD下发充电或停止(CMD=82)设备号【" + pile.getPileNo() + "】报文:" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = channel.writeAndFlush(buf);
        log.info("JMD-测试={}",replyMsg);
        log.info("下发充电={}",future.isSuccess());
    }

    @Override
    public void testJsy10charge(String pileNo, String gunNo, Integer isOpen) {
        long id = IdentityIdUtil.getFlowIdInstance().nextId();
        String orderNo = String.valueOf(id);
        Channel channel = getChannel(pileNo);
        if(channel==null){
            throw new RuntimeException("channel不能为空");
        }
        if(isOpen>0){
            Integer maxChargeTime = 100;                  //最大充电时间(分钟)
            StringBuilder builder = new StringBuilder();
            builder.append("_" + JSYConstant.Cmd.S00201).append("/0027:");
            builder.append(gunNo + "/")
                    .append(StringUtils.leftPad(String.valueOf(maxChargeTime), 4, "0"))
                    .append("/")
                    .append(StringUtils.leftPad(Long.toHexString(Long.valueOf(orderNo)), 16, "0"))
                    .append("/02") //刷卡01或者服务器命令02
                    .append("\r\n");
            log.info("JMD-4/10口测试={}",builder.toString());
            ByteBuf buf = Unpooled.buffer(builder.toString().getBytes().length);
            buf.writeBytes(builder.toString().getBytes());
            ChannelFuture future = channel.writeAndFlush(buf);
            System.out.println(future);
        }else{
            //下发停止充电
            StringBuilder builder = new StringBuilder();
            builder.append("_"+ JSYConstant.Cmd.S00203).append("/0002:");
            builder.append(gunNo)
                    .append("\r\n");
            log.info("JMD-JSY设备号：【" + pileNo + "】停止充电(CMD = S00203):" + builder.toString());
            ByteBuf buf = Unpooled.buffer(builder.toString().getBytes().length);
            buf.writeBytes(builder.toString().getBytes());
            ChannelFuture future = channel.writeAndFlush(buf);
        }
    }


    @Override
    public void testJsy2Time(String pileNo, String gunNo) {
        RechargePile pile = new RechargePile();
        Channel channel = getChannel(pileNo);
        if(channel==null){
            throw new RuntimeException("channel不能为空");
        }
        Integer maxChargeTime = 36000; //最大充电时间 10小时
        pile.setCode(5);
        pile.setPileNo(pileNo);
        StringBuilder payload = new StringBuilder();
        payload.append("01")
                .append(pileNo)
                .append(CommonUtil.reverseHex(org.apache.commons.lang3.StringUtils.leftPad(Integer.toHexString(maxChargeTime),4,"0")));
        String replyMsg = ZDObjectUtil.ydsplicingMessage(payload.toString(), pile,gunNo, BizConstant.Cmd.CMD_YD_8a);
        log.info("JMD-YD下发服务器设备号【"+pileNo+","+gunNo+"】修改充电时长/电量:"+maxChargeTime+"秒，功率：(CMD=8a)报文:" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = channel.writeAndFlush(buf);
    }

    @Override
    public void testJsy2Bomb(String pileNo, String gunNo) {
        RechargePile pile = new RechargePile();
        Channel channel = getChannel(pileNo);
        if(channel==null){
            throw new RuntimeException("channel不能为空");
        }
        pile.setCode(5);
        pile.setPileNo(pileNo);
        StringBuilder payload = new StringBuilder();
        String replyMsg = ZDObjectUtil.ydsplicingMessage(payload.toString(), pile, gunNo, BizConstant.Cmd.CMD_JSY_33);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = channel.writeAndFlush(buf);
        log.info("JMD-33测试修改时长={}",replyMsg);
        log.info("下发充电={}",future.isSuccess());
    }

    @Override
    public String testJsy10online(String pileNo) {
        Channel channel = getChannel(pileNo);
        if(channel==null){
            return "未在线";
        }
        return "在线";
    }


    private Channel getChannel(String pileNo) {
        Map<String, ChannelId> channels = (Map<String, ChannelId>) redisUtil.get(CacheNames.TCP_CHANNEL);
        if (channels == null || channels.get(pileNo) == null) {
            log.error(pileNo + ":未找到相关发送通道ID");
            return null;
        }
        log.info("==========" + channels.get(pileNo) + "------" + pileNo + "=============");
        ChannelId channelId = channels.get(pileNo);
        Channel channel = TcpInboundAdapter.clients.find(channelId);
        if (channel == null) {
            log.error(pileNo + ":未找到相关发送通道");
            return null;
        }
        log.error("设备号:" + pileNo + "channel:" + channel.id() + "是否active:" + channel.isActive());
        return channel;
    }

    @Override
    public String testIssueModel(String pileNo, String gunNo) {
        List<TimeModel> timeModels = JSON.parseArray("[{\"id\":\"1645334706342780929\",\"modelId\":\"1645334706313420801\",\"startTime\":\"00:00\",\"endTime\":\"00:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706351169538\",\"modelId\":\"1645334706313420801\",\"startTime\":\"00:30\",\"endTime\":\"01:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706355363841\",\"modelId\":\"1645334706313420801\",\"startTime\":\"01:00\",\"endTime\":\"01:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706359558145\",\"modelId\":\"1645334706313420801\",\"startTime\":\"01:30\",\"endTime\":\"02:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706367946753\",\"modelId\":\"1645334706313420801\",\"startTime\":\"02:00\",\"endTime\":\"02:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706372141058\",\"modelId\":\"1645334706313420801\",\"startTime\":\"02:30\",\"endTime\":\"03:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706376335362\",\"modelId\":\"1645334706313420801\",\"startTime\":\"03:00\",\"endTime\":\"03:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706380529665\",\"modelId\":\"1645334706313420801\",\"startTime\":\"03:30\",\"endTime\":\"04:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706384723969\",\"modelId\":\"1645334706313420801\",\"startTime\":\"04:00\",\"endTime\":\"04:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706388918274\",\"modelId\":\"1645334706313420801\",\"startTime\":\"04:30\",\"endTime\":\"05:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706393112578\",\"modelId\":\"1645334706313420801\",\"startTime\":\"05:00\",\"endTime\":\"05:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706397306881\",\"modelId\":\"1645334706313420801\",\"startTime\":\"05:30\",\"endTime\":\"06:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706405695489\",\"modelId\":\"1645334706313420801\",\"startTime\":\"06:00\",\"endTime\":\"06:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706409889793\",\"modelId\":\"1645334706313420801\",\"startTime\":\"06:30\",\"endTime\":\"07:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706414084097\",\"modelId\":\"1645334706313420801\",\"startTime\":\"07:00\",\"endTime\":\"07:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706418278401\",\"modelId\":\"1645334706313420801\",\"startTime\":\"07:30\",\"endTime\":\"08:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706422472705\",\"modelId\":\"1645334706313420801\",\"startTime\":\"08:00\",\"endTime\":\"08:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706426667009\",\"modelId\":\"1645334706313420801\",\"startTime\":\"08:30\",\"endTime\":\"09:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706430861314\",\"modelId\":\"1645334706313420801\",\"startTime\":\"09:00\",\"endTime\":\"09:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706439249922\",\"modelId\":\"1645334706313420801\",\"startTime\":\"09:30\",\"endTime\":\"10:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706443444225\",\"modelId\":\"1645334706313420801\",\"startTime\":\"10:00\",\"endTime\":\"10:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706447638529\",\"modelId\":\"1645334706313420801\",\"startTime\":\"10:30\",\"endTime\":\"11:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706451832833\",\"modelId\":\"1645334706313420801\",\"startTime\":\"11:00\",\"endTime\":\"11:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706456027137\",\"modelId\":\"1645334706313420801\",\"startTime\":\"11:30\",\"endTime\":\"12:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706460221442\",\"modelId\":\"1645334706313420801\",\"startTime\":\"12:00\",\"endTime\":\"12:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706464415745\",\"modelId\":\"1645334706313420801\",\"startTime\":\"12:30\",\"endTime\":\"13:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706472804353\",\"modelId\":\"1645334706313420801\",\"startTime\":\"13:00\",\"endTime\":\"13:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706476998658\",\"modelId\":\"1645334706313420801\",\"startTime\":\"13:30\",\"endTime\":\"14:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706481192961\",\"modelId\":\"1645334706313420801\",\"startTime\":\"14:00\",\"endTime\":\"14:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706485387266\",\"modelId\":\"1645334706313420801\",\"startTime\":\"14:30\",\"endTime\":\"15:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706489581570\",\"modelId\":\"1645334706313420801\",\"startTime\":\"15:00\",\"endTime\":\"15:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706493775874\",\"modelId\":\"1645334706313420801\",\"startTime\":\"15:30\",\"endTime\":\"16:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706497970178\",\"modelId\":\"1645334706313420801\",\"startTime\":\"16:00\",\"endTime\":\"16:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706506358786\",\"modelId\":\"1645334706313420801\",\"startTime\":\"16:30\",\"endTime\":\"17:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706510553089\",\"modelId\":\"1645334706313420801\",\"startTime\":\"17:00\",\"endTime\":\"17:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706514747394\",\"modelId\":\"1645334706313420801\",\"startTime\":\"17:30\",\"endTime\":\"18:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706518941698\",\"modelId\":\"1645334706313420801\",\"startTime\":\"18:00\",\"endTime\":\"18:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706523136001\",\"modelId\":\"1645334706313420801\",\"startTime\":\"18:30\",\"endTime\":\"19:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706527330305\",\"modelId\":\"1645334706313420801\",\"startTime\":\"19:00\",\"endTime\":\"19:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706531524609\",\"modelId\":\"1645334706313420801\",\"startTime\":\"19:30\",\"endTime\":\"20:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706535718913\",\"modelId\":\"1645334706313420801\",\"startTime\":\"20:00\",\"endTime\":\"20:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706544107522\",\"modelId\":\"1645334706313420801\",\"startTime\":\"20:30\",\"endTime\":\"21:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706548301825\",\"modelId\":\"1645334706313420801\",\"startTime\":\"21:00\",\"endTime\":\"21:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706552496130\",\"modelId\":\"1645334706313420801\",\"startTime\":\"21:30\",\"endTime\":\"22:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706556690434\",\"modelId\":\"1645334706313420801\",\"startTime\":\"22:00\",\"endTime\":\"22:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706560884737\",\"modelId\":\"1645334706313420801\",\"startTime\":\"22:30\",\"endTime\":\"23:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706565079041\",\"modelId\":\"1645334706313420801\",\"startTime\":\"23:00\",\"endTime\":\"23:30\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0},{\"id\":\"1645334706569273346\",\"modelId\":\"1645334706313420801\",\"startTime\":\"23:30\",\"endTime\":\"00:00\",\"chargeFee\":-1,\"serviceFee\":-1,\"dictCode\":0}]",TimeModel.class);
        List<ChargeRule> chargeRules = JSON.parseArray("[{\"id\":\"1645334706619604993\",\"siteId\":\"-1\",\"modelId\":\"1645334706313420801\",\"dictCode\":0,\"dictName\":\"尖时\",\"electricFee\":1.1,\"serviceFee\":0.5,\"tenantId\":\"\",\"orgCode\":\"\"},{\"id\":\"1645334706623799298\",\"siteId\":\"-1\",\"modelId\":\"1645334706313420801\",\"dictCode\":1,\"dictName\":\"峰时\",\"electricFee\":1.1,\"serviceFee\":0.5,\"tenantId\":\"\",\"orgCode\":\"\"},{\"id\":\"1645334706627993601\",\"siteId\":\"-1\",\"modelId\":\"1645334706313420801\",\"dictCode\":2,\"dictName\":\"平时\",\"electricFee\":1.1,\"serviceFee\":0.5,\"tenantId\":\"\",\"orgCode\":\"\"},{\"id\":\"1645334706632187905\",\"siteId\":\"-1\",\"modelId\":\"1645334706313420801\",\"dictCode\":3,\"dictName\":\"谷时\",\"electricFee\":1.1,\"serviceFee\":0.5,\"tenantId\":\"\",\"orgCode\":\"\"}]",ChargeRule.class);
        String payload = yxIssueModel(chargeRules, timeModels);
        StringBuilder hexstring = new StringBuilder();
        String deviceNo = pileNo;
        //桩编码
        hexstring.append(StringUtils.leftPad(pileNo, 14, '0'));
        //计费模型编号
        hexstring.append("4185");
        hexstring.append(payload);
        //Channel channel = TcpInboundAdapter.clients.find(channels.get(deviceNo));
        Channel channel = getChannel(deviceNo);
        String sendMessage = YxObjectUtil.pushMessage(hexstring.toString(),  YxConstant.Cmd.CMD_58);
        log.info("发送JMD-YX报文[" + pileNo + "," + gunNo + "](CMD=58): ");
        log.info(sendMessage);
        ByteBuf buf = Unpooled.buffer(sendMessage.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(sendMessage));
        ChannelFuture success13 = channel.writeAndFlush(buf);
        return "下发计费模型成功";
    }

    @Override
    public String testCharge(String pileNo, String gunNo) {
        Channel channel = getChannel(pileNo);
        log.info("汽车开始启动下发充电，桩编号 : " + pileNo + "枪号 : " + gunNo);
        long id = IdentityIdUtil.getFlowIdInstance().nextId();
        if (channel != null) {
            log.info("获取到Channel");
            RemoteStartDto dto = new RemoteStartDto();
            dto.setGunNo(gunNo);
            dto.setBalance(100); //TODO 要根据实际余额填写
            dto.setPileNo(pileNo);
            dto.setTradeNo(String.valueOf(id));
            String json = JSON.toJSONString(dto);
            log.info("JSON串 : " + json);
            yxRemoteStartService.doExecute(CommonUtil.stringToBytes(json), channel);
        }
        return "下发成功";
    }

    @Override
    public String testStopCharge(String pileNo, String gunNo) {
        StringBuilder payload = new StringBuilder();
        //获取充电枪
        payload.append(StringUtils.leftPad(pileNo,14,"0"))
                .append(gunNo);
        String replyMsg = YxObjectUtil.pushMessage(payload.toString(), YxConstant.Cmd.CMD_36);
        log.info("JMD-YX下发停止充电(CMD=36)报文:" + replyMsg);
        Channel channel = getChannel(pileNo);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = channel.writeAndFlush(buf);
        return "下发停止充电成功";
    }



    private static String yxIssueModel(List<ChargeRule> chargeRules, List<TimeModel> timeModelList) {
        StringBuilder payload = new StringBuilder();
        StringBuilder rulesb = new StringBuilder();
        StringBuilder timesb = new StringBuilder();
        for (int j = 0; j < chargeRules.size(); j++) {
            BigDecimal electricFee = BigDecimal.valueOf(chargeRules.get(j).getElectricFee()).multiply(BigDecimal.valueOf(100000));
            String hexElectricFee = CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(electricFee.intValue()), 8, "0"));
            BigDecimal serviceFee = BigDecimal.valueOf(chargeRules.get(j).getServiceFee()).multiply(BigDecimal.valueOf(100000));
            String hexServiceFee = CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(serviceFee.intValue()), 8, "0"));
            rulesb.append(hexElectricFee).append(hexServiceFee);
        }
        for (int i = 0; i < timeModelList.size(); i++) {
            String dictCode = StringUtils.leftPad(StrUtil.toString(timeModelList.get(i).getDictCode()), 2, "0");
            timesb.append(dictCode);
        }
        payload.append(rulesb);
        payload.append("00");
        payload.append(timesb);
        return payload.toString();
    }
}
